package com.huige.api;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 朱志辉
 * @Create 2024/7/19 下午1:45
 * @Version 1.0
 * Description:
 */
@Controller
public class ApiController {

    @Autowired  //ioc容器获取对应的类型实体对象(组件) 并自动装配
    private ServletContext servletContext;


    public String getApiKey(HttpServletResponse response,
                            HttpServletRequest request,
                            HttpSession session) {
        //使用原生对象就可以了
        //ServletContext 【1.最大的配置文件  2.全局最大共享域 3.核心api getRealPath】
        //方案1：request获取 session获取
        ServletContext servletContext = request.getServletContext();
        ServletContext servletContext1 = session.getServletContext();
        //方案2：ServletContext 会自动转入ioc容器  程序启动servletContext  -  ioc容器
        //直接全局注入
        return servletContext.getInitParameter("apiKey");
    }
}
